package com.example.mymarket.Fragment;

import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.os.Parcelable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridLayout;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.example.mymarket.Data_model.CatalogData;
import com.example.mymarket.Data_model.CurrentCategory;
import com.example.mymarket.Data_model.CurrentData;
import com.example.mymarket.Data_model.ResponseData;
import com.example.mymarket.Data_model.currentSubCategory;
import com.example.mymarket.R;
import com.example.mymarket.activity.CategoryActivity;
import com.example.mymarket.adapter.currentSubCategoryAdapter;
import com.example.mymarket.api.ApiService;
import com.example.mymarket.interfaces.ResponseCallback;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link CatalogContentFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class CatalogContentFragment extends Fragment {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    private ImageView content_img ;
    private TextView content_text;
    private GridView gridView;
    private int categoryId;
    private String categoryName;
    private String categoryDesc;
    private String categoryPicUrl;

    public CatalogContentFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment CatalogContentFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static CatalogContentFragment newInstance(String param1, String param2) {
        CatalogContentFragment fragment = new CatalogContentFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    private void initView(View view) {
        content_img = view.findViewById(R.id.fragment_catalog_content_img);
        content_text = view.findViewById(R.id.fragment_catalog_content_text);
        gridView = view.findViewById(R.id.fragment_catalog_content_grid);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
            categoryId = getArguments().getInt("id");
            Log.i("TAG", "categoryId= " + categoryId);
            categoryName = getArguments().getString("name");
            categoryDesc = getArguments().getString("desc");
            categoryPicUrl = getArguments().getString("picUrl");

        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_catalog_content, container, false);
        initView(view);
        content_text.setText(categoryDesc);
        Glide.with(content_img)
                .load(categoryPicUrl)
                .apply(RequestOptions.bitmapTransform(new RoundedCorners(30)))
                .into(content_img);
        ApiService.getCurrent(categoryId, new ResponseCallback<ResponseData<CurrentData>>() {
            @Override
            public void onSuccess(ResponseData<CurrentData> result) {
                List<CurrentCategory> currentCategoryList = result.getData().getCurrentSubCategory();
                if (result != null){
                    gridView.setAdapter(new currentSubCategoryAdapter(requireContext(),currentCategoryList));
                    gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                            CurrentCategory Category = result.getData().getCurrentSubCategory().get(i);
                            Intent intent = new Intent(requireActivity(), CategoryActivity.class);
                            intent.putExtra("categoryId", Category.getId());
                            requireContext().startActivity(intent);
                        }
                    });
                }
            }
        });

        return view;
    }
}